import { _decorator, Component, Node, Sprite, SpriteFrame, Size, UITransform } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('ImageArranger')
export class ImageArranger extends Component {

    @property([SpriteFrame])
    images: SpriteFrame[] = []; // 存储图片的 SpriteFrame

    @property(Node)
    container: Node = null; // 图片容器节点

    @property
    spacing: number = 10; // 图片之间的间距

    // 在组件加载时排列图片
    onLoad() {
        this.arrangeImages();
    }

    private arrangeImages() {
        if (!this.container || this.images.length === 0) return;

        // 清空容器内的所有子节点
        this.container.removeAllChildren();

        // 添加每张图片
        for (let i = 0; i < this.images.length; i++) {
            const spriteNode = new Node(`Image${i}`);
            const sprite = spriteNode.addComponent(Sprite);
            sprite.spriteFrame = this.images[i];

            // 添加 UITransform 组件以设置大小
            const uiTransform = spriteNode.addComponent(UITransform);
            const spriteSize: Size = sprite.spriteFrame.originalSize; // 获取原始尺寸
            uiTransform.setContentSize(200, 200); // 设置节点大小

            // 设置位置
            spriteNode.setPosition(0,-690+ i * (200 + this.spacing), 0); // 纵向排列
            this.container.addChild(spriteNode); // 将 Sprite 添加到容器
        }
    }
}